package weatehrcrowd.servlets;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import weathercrowd.managers.PersistenceManager;
import weathercrowd.model.Location;
import weathercrowd.model.Rainfall;
import weathercrowd.model.Temperature;
import weathercrowd.utilities.SessionHelper;

/**
 * Servlet implementation class QueryTestServlet
 */
@WebServlet("/QueryTestServlet")
public class QueryTestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public QueryTestServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		PersistenceManager pm = SessionHelper.getPersistenceManager(session);
		
		// get specific records
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		Date date = null;
		try {
			date = formatter.parse("19960101");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<Rainfall> rainfalls = pm.getRainfalls(date);
		List<Temperature> temperatures = pm.getTemperatures(date);
		Location sampleLocation = null;
		for (Rainfall rf : rainfalls) {
			System.out.printf("rainfall amount at %s : %f\n", rf.getLocation().getName(), rf.getAmount());
			if (sampleLocation  == null) sampleLocation  = rf.getLocation();
		}
		for (Temperature t : temperatures) {
			System.out.printf("temperature min at %s: %f, max: %f\n", t.getLocation().getName(), t.getMin(), t.getMax());
		}
		
		
		// get records for a specific date range
		Date start = null, end = null;
		try {
			start = formatter.parse("19960101");
			end = formatter.parse("19981231");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String xmlRf = pm.getRainfallMonthlyXml(start, end, sampleLocation);
		String xmlTp = pm.getTemperatureMonthlyXml(start, end, sampleLocation);
		
		System.out.printf("Rainfall data from 19960101 to 19981231:\n%s\n", xmlRf);
		System.out.printf("Temperature data from 19960101 to 19981231:\n%s\n", xmlTp);
	}

}
